package com.uroad.jilinpda.widget;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uroad.jilinpda.R;
import com.uroad.jilinpda.util.SupDialogStaticUtils;


/**
 * ======================================================
 * Created by Administrator able_fingerth on 2016/10/28.
 * <p/>
 * 版权所有，违者必究！
 * <详情描述/>
 */
public class SYSDiaLogUtils {


    ////1.成功  2.提示   3.錯誤  確認
    private static String loadingStr = "正在加載...";
    private static String successStr = "成功";
    private static String tipStr = "提示";
    private static String errorStr = "錯誤";
    private static String confirmStr = "确定";
    private static String cancelStr = "取消";
    private static ProgressDialog progressDialog;
    private static AlertDialog progressAlertDialog;
    static Animation operatingAnim;
    private static TimeCountController timeCountController;
    //4 取消 確認 對話框
    private static AlertDialog confirmDialog;
    private static AlertDialog confirmDialognext;
    private static AlertDialog confirmDialogNew;
    public static Toast toast;
    public enum SYSDiaLogType {
        DefaultTpye,//默認的樣式，系統樣式
        IosType,//ios風格的
        HorizontalWithNumberProgressBar,  // 進度條
        RoundWidthNumberProgressBar,     // 圓形進度條
        ;
    }

    public enum SYSConfirmType {
        Success,//成功 綠色
        Tip,//提示  黃黃的
        Warning //警告 紅色
    }

    /**
     * 新的對話框彈出之前，先把舊的清除掉
     */
    private static void initDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        if (progressAlertDialog != null && progressAlertDialog.isShowing()) {
            progressAlertDialog.dismiss();
            progressAlertDialog = null;
        }
        if (confirmDialog != null && confirmDialog.isShowing()) {
            confirmDialog.dismiss();
            confirmDialog = null;
        }
        if (confirmDialognext != null && confirmDialognext.isShowing()) {
            confirmDialognext.dismiss();
            confirmDialognext = null;
        }
    }

    public static void showConfirmDialogType(Activity context,
                                             String title,String titleNext, String msg,String cancle,String sure,
                                             final ConfirmDialogListener confirmDialogListener) {
        showConfirmAlertDialogType(context, title,titleNext,msg, cancle, sure, false, true, confirmDialogListener);
    }
    private static void showConfirmAlertDialogType(Activity context,
                                                   String title, String titleNext, String msg, String confirmStr, String cancelStr,
                                                   boolean canceledOnTouchOutside, boolean cancelable,
                                                   final ConfirmDialogListener confirmDialogListener) {
        if (context == null || context.isFinishing()) {
            return;
        }
        closeKeyboardHidden(context);
        initDialog();

        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog_Styles);
        View dialogView = View.inflate(context, R.layout.dialog_confirm_alert_type, null);
        LinearLayout dialog_view_layout = (LinearLayout) dialogView.findViewById(R.id.line_first);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) dialog_view_layout.getLayoutParams();
        params.width = SupDialogStaticUtils.getScreenWidth(context) * 6 / 7;
        dialog_view_layout.setLayoutParams(params);
        Button confirm = (Button) dialogView.findViewById(R.id.confirm);
        Button cancel = (Button) dialogView.findViewById(R.id.cancel);
        if (TextUtils.isEmpty(confirmStr)) {
            confirmStr = SYSDiaLogUtils.confirmStr;
        }
        if (TextUtils.isEmpty(cancelStr)) {
            cancelStr = SYSDiaLogUtils.cancelStr;
        }
        confirm.setText(confirmStr);
        cancel.setText(cancelStr);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(false, true);
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(true, false);
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });
        TextView title_tv = (TextView) dialogView.findViewById(R.id.title_tv);
        if (TextUtils.isEmpty(title)) {
            title_tv.setVisibility(View.GONE);
        } else {
            title_tv.setVisibility(View.VISIBLE);
            title_tv.setText(title);
        }
        TextView title_tv_next = (TextView) dialogView.findViewById(R.id.title_tv_next);
        if (TextUtils.isEmpty(titleNext)) {
            title_tv_next.setVisibility(View.GONE);
        } else {
            title_tv_next.setVisibility(View.VISIBLE);
            title_tv_next.setText(titleNext);
        }

        TextView message_tv = (TextView) dialogView.findViewById(R.id.message_tv);
        if (TextUtils.isEmpty(msg)) {
            message_tv.setVisibility(View.GONE);
        } else {
            message_tv.setText(msg);
        }

        builder.setView(dialogView);
        confirmDialog = builder.create();
        confirmDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        confirmDialog.setCancelable(cancelable);
        confirmDialog.show();
    }

    public static void showConfirmPhotoDialog(Activity context,
                                              final ConfirmDialogListener confirmDialogListener) {
        showPhotoDialog(context,confirmDialogListener);
    }
    /**
     *  遮罩下面控件点击是否可以點擊(弹出选择拍照相册)
     */
    private static void showPhotoDialog(Activity context,final ConfirmDialogListener confirmDialogListener){
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        //填充对话框的布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_select_pic, null);
        //初始化控件
        TextView camera = (TextView) inflate.findViewById(R.id.camera);
        TextView pic = (TextView) inflate.findViewById(R.id.pic);
        TextView cancel = (TextView) inflate.findViewById(R.id.cancel);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(true, false);
                }
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(false, true);
                }
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        lp.y = 20;//设置Dialog距离底部的距离
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialogWindow.setWindowAnimations(R.style.Animation_Bottom_Rising);
        dialog.show();//显示对话框

    }

    public static void showOnlyTextDialog(Activity context, String title, String content) {
        if (context == null || context.isFinishing()) {
            return;
        }
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        View dialogView = View.inflate(context, R.layout.dialog_only_text, null);
        LinearLayout line_first = (LinearLayout) dialogView.findViewById(R.id.line_first);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) line_first.getLayoutParams();
        params.width = SupDialogStaticUtils.getScreenWidth(context) * 6/7;
        line_first.setLayoutParams(params);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText(title);
        TextView tv_content = dialogView.findViewById(R.id.tv_content);
        tv_content.setText(content);
        ImageView iv_dialog_close = dialogView.findViewById(R.id.iv_dialog_close);
        iv_dialog_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        //将布局设置给Dialog
        dialog.setContentView(dialogView);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //获得窗体的属性
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
////        lp.y = 20;//设置Dialog距离底部的距离
////       将属性设置给窗体
//        dialogWindow.setAttributes(lp);
        dialogWindow.setWindowAnimations(R.style.Animation_Bottom_Rising);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();//显示对话框
    }
    public static void showConfirmDialogType1(Activity context,
                                              String title,String titleNext,String confirmStr,
                                              final ConfirmDialogListener confirmDialogListener) {
        showConfirmAlertDialogType1(context, title,titleNext, confirmStr, false, true, confirmDialogListener);
    }
    private static void showConfirmAlertDialogType1(Activity context,
                                                    String title, String titleNext,String confirmStr,
                                                    boolean canceledOnTouchOutside, boolean cancelable,
                                                    final ConfirmDialogListener confirmDialogListener) {
        if (context == null || context.isFinishing()) {
            return;
        }
        closeKeyboardHidden(context);
        initDialog();

        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog_Styles);
        View dialogView = View.inflate(context, R.layout.dialog_confirm_alert_type1, null);
        LinearLayout dialog_view_layout = (LinearLayout) dialogView.findViewById(R.id.line_first);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) dialog_view_layout.getLayoutParams();
        params.width = SupDialogStaticUtils.getScreenWidth(context) * 6/7;
        dialog_view_layout.setLayoutParams(params);
        Button confirm = (Button) dialogView.findViewById(R.id.confirm);
        if (TextUtils.isEmpty(confirmStr)) {
            confirmStr = SYSDiaLogUtils.confirmStr;
        }
        confirm.setText(confirmStr);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(false, true);
                }
                if (confirmDialogNew != null && confirmDialogNew.isShowing()) {
                    confirmDialogNew.dismiss();
                }
            }
        });
        TextView title_tv = (TextView) dialogView.findViewById(R.id.title_tv);
        if (TextUtils.isEmpty(title)) {
            title_tv.setVisibility(View.GONE);
        } else {
            title_tv.setVisibility(View.VISIBLE);
            title_tv.setText(title);
        }
        TextView title_tv_next = (TextView) dialogView.findViewById(R.id.title_tv_next);
        if (TextUtils.isEmpty(titleNext)) {
            title_tv_next.setVisibility(View.GONE);
        } else {
            title_tv_next.setVisibility(View.VISIBLE);
            title_tv_next.setText(titleNext);
        }
        builder.setView(dialogView);
        confirmDialogNew = builder.create();
        confirmDialogNew.setCanceledOnTouchOutside(canceledOnTouchOutside);
        confirmDialogNew.setCancelable(cancelable);
        confirmDialogNew.show();
    }
    /**
     * 隐藏软键盘
     **/
    private static void closeKeyboardHidden(Activity context) {
        View view = context.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public interface ConfirmDialogListener {
        void onClickButton(boolean clickLeft, boolean clickRight);
    }

    public interface ConfirmDialogTextListener {
        void onClickButton(boolean clickLeft, boolean clickRight, String nameHost);
    }

    public interface SureListener {
        void sureButton(String cardNo);
    }
}
